//@see next js function is for user query add edit delete 10:39 2008-2-22

/**
* @todo：用户查询
* @date：10:39 2008-2-22
* @author：hyh
*/
/** modified by pxw 9:46 2008-03-04 */
function loadAcluserList(page,pageSize){
	if(typeof(page) == "undefined") page = 1;
	if(typeof(pageSize) == "undefined") pageSize = 15;
	var oJSUserSearchBean = new JSUserSearchBean();
	getFormValue(oJSUserSearchBean);
	DWRUtil.useLoadingMessage("正在读取数据...");
	ConfigAction.listAcluser(oJSUserSearchBean,page,pageSize,function(pageObject){
		  var data = pageObject.resultList;
		  var rowNum = 1;
		  var i = 1;
		  var url;
		  DWRUtil.removeAllRows("user_table");
		  var cellFuncs = [
		  function(data) {
			  return "<input type=\"checkbox\" name=\"userchkbox\" value=\""+data.userid+"\" class=\"checkbox\">";
		  },
		  function(data) {//用户帐号
			  if(data.account == "null" || data.account == null){
				  return "";
			  }else{
				  return data.account;
			  }
		  },
		  function(data) {//用户名称
			  if(data.realname == "null" || data.realname == null){
				  return "";
			  }else{
				  return data.realname;
			  }	  
		  },
		  	 /*
		  function(data) {//用户部门
			  if(data.orgname == "null" || data.orgname == null){
				  return "";
			  }else{
				  return data.orgname;
			  }	  
		  },*/
		  function(data) {//用户状态
			  if(data.userstatus == "null" || data.userstatus == null){
				  return "";
			  }else if(data.userstatus == "1"){
			  	  return "激活";
			  }else if(data.userstatus == "2"){
			  	  return "未激活";
			  }else{
			  	  return "已删除";
			  }
		  },
		  function(data) {//是否在线
			  if(data.isonline == "null" || data.isonline == null){
				  return "";
			  }else if(data.isonline == "1"){
			  	  return "在线";
			  }else{
			  	  return "未在线";
			  }
		  },
		  function(data) {//创建日期
			  if(data.createdate == "null" || data.createdate == null){
				  return "";
			  }else{
				  return formatDate(data.createdate);
			  }	  
		  }
		  	/*,
		   function(data) {//小灵通
			  if(data.pas == "null" || data.pas == null){
				  return "";
			  }else{
				  return data.pas;
			  }	  
		  },
		   function(data) {//手机
			  if(data.mobile == "null" || data.mobile == null){
				  return "";
			  }else{
				  return data.mobile;
			  }	  
		  }*/,
		   function(data) {//联系电话
			  if(data.telphone == "null" || data.telphone == null){
				  return "";
			  }else{
				  return data.telphone;
			  }	  
		  },
		  function(data) {
			return "<a href=\"#\" onclick=\"openAcluserop('"+data.userid+"');return false;\"><img src=\"../themes/default/images/edit2.gif\" border=\"0\" width=\"15\" height=\"15\" valign=\"bottom\" alt=\"编辑\"></a>";
			//<a href=\"#\" onclick=\"resetacluserpassword();\"><img src=\"../themes/default/images/shou2.gif\" border=\"0\" width=\"15\" height=\"15\" valign=\"bottom\" alt=\"密码重置\">
		  }
		  ];
		  DWRUtil.addRows("user_table",data,cellFuncs,{
		  rowCreator:function(options) {
		    var row = document.createElement("tr");
		    return row;
		  },
		  cellCreator:function(options) {
		    var td = document.createElement("td");
		    var cellNum = options.cellNum;
		    td.align = "center";
			//td.height = "28";
		    return td;
		  },
		  escapeHtml:false
		  });
		  /**分页-----------------------------------------------------*/
		  var func_ = "loadAcluserList(page,15)";
		  var htmlStr = showPage(pageObject,func_);//对象和方法
		  DWRUtil.setValue("user_page_div",htmlStr,{escapeHtml:false});
		  addTableRolloverEffect('table_user_query','tableRollOverEffect1','tableRowClickEffect1');
		  initSortTable('table_user_query',Array('','S','S','S','S','S','S','S','S','S',''));
	});
}

//open add or edit or detail page
function openAcluserop(id){
	var url = "acluserop.html";
	if(id){
		url += "?id=" + id +　"&pagetype=user";
	}
	openWin_h(url,"800","500");
}

/**
* @todo：删除用户（不用，改为设置用户状态为3）
* @date：9:25 2008-2-25
* @author：hyh

function deleteUser(){
	var userIds = getCheckValuesByName("userchkbox");
	if(userIds == ""){
		alert("请至少要选择一条删除的用户记录！");
		return false;
	}
	if(confirm("你确定删除用户吗？")){
		DWRUtil.useLoadingMessage("正在删除用户数据...");
		ConfigAction.deleteAcluser(userIds,function(data){
			alert(data);
			loadAcluserList();
		});
	}
}
*/
/**
* @todo：用户密码重置
* @date：2008-12-18
* @author：wangchen
*/
function resetUserPassword(){
    var ids = getCheckValuesByName("userchkbox");
	if(ids == ""){
		alert("请至少选择一条用户记录!");
		return false;
	}
    if(confirm("确定重置该用户的密码么？")){
    	DWRUtil.useLoadingMessage("正在设置数据，请稍后...");
	  	ConfigAction.resetUserPassword(ids,function(data){alert(data);});
  	}
}

/**
* @todo：设置用户状态：1 == 激活、2 == 未激活，3 == 删除
* @date：2008-12-18
* @author：wangchen
*/
function setUserStatus(type){
	var ids = getCheckValuesByName("userchkbox");
	if(ids == ""){
		alert("请至少选择一条用户记录!");
		return false;
	}
	var info = (type=='1'?'激活吗？':(type=='2'?'停用吗？':'删除吗?'));
	if(confirm("确定将此用户"+info)){
		DWRUtil.useLoadingMessage("正在设置数据，请稍后...");
		ConfigAction.setUserStatus(ids,type,function(data){alert(data);loadAcluserList();});
	}
}
		
		
//@see next js function is for user add or edit or detail for acluserop.html 10:39 2008-2-22

/**
* @todo：用户保存（添加或编辑）
* @date：10:39 2008-2-22
* @author：hyh
*/
function userOpInit(){
	var id = JSRequest("id");
	DWRUtil.setValue("deptid",JSRequest("deptid") || 2);//部门传值
	DWRUtil.setValue("deptname",JSRequest("deptname"));
	
	listAllRole(id);//role[]
	DWREngine.setAsync(false);
	listAllDuty();
	if(id != null && id != ""){//edit or detail
		setUserDetailHtml();
		//用户信息及部门
		DWRUtil.useLoadingMessage("正在读取用户信息...");
		ConfigAction.getAclusersByUserId(id,function(oJSUserBean){
			//oJSUserBean.createdate = formatDate(oJSUserBean.createdate);
			//oJSUserBean.lastmodifydate = formatDate(oJSUserBean.lastmodifydate);
			setFormValue(oJSUserBean);
		});
		ConfigAction.getAclorganizationByUserId(id,function(data){
			DWRUtil.setValue("deptid",data.id);
			DWRUtil.setValue("deptname",data.deptname);
		});
		ConfigAction.getAclrelauserdutyByUserId(id,function(data){
			if(data) DWRUtil.setValue("dutyid",data.dutyid);
		});
		DWREngine.setAsync(true);
	}
}

/**
* @todo：列出所有职务
* @date：10:39 2008-2-22
* @author：hyh
*/
function listAllDuty(){
	DWRUtil.useLoadingMessage("正在读取职务数据...");
	ConfigAction.listAllDuty(function(data){
		DWRUtil.removeAllOptions("dutyid");
		DWRUtil.addOptions("dutyid",[{ name:'--------------------------', id:''}],"id","name");
	  	DWRUtil.addOptions("dutyid",data,"id","dutyname");
	});
}

/**
* @todo：列出所有角色
* @date：10:39 2008-2-22
* @author：hyh
*/
function listAllRole(id){
	DWRUtil.useLoadingMessage("正在读取角色数据...");
	var html = "<input name=\"rolechkall\" type=\"checkbox\" onclick=\"chooseAll('rolechkbox')\" class=\"checkbox\">角色列表(全选):<br>";
	ConfigAction.listAllAclrole(function(data){
		for(var i=0;i<data.length;i++){
			html += "<input name=\"rolechkbox\" type=\"checkbox\" value=\""+data[i].id+"\" class=\"checkbox\">" + data[i].rolename;
			/**
			if(i!=0 && (i+1)%3==0){
				html += "<br>"
			}
			*/
		}
		$("role_list").innerHTML = html;
		if(id != null && id != ""){//load role
			ConfigAction.listAclrelaroleuserByUserId(id,function(data){
				var len = data.length;
				for(var i=0;i<len;i++){
					var chkArray = document.getElementsByName("rolechkbox");
					var chkLen = chkArray.length;
					if(len == chkLen){//全选
						$("rolechkall").checked = true;
						$("rolechkall").disabled = true;
						for(var j=0;j<chkLen;j++ ){
							chkArray[j].checked = true;
							chkArray[j].disabled = true;
       			 		}//for
						break;
					}else{
						for(var j=0;j<chkLen;j++ ){
							chkArray[j].disabled = true;
							if(chkArray[j].value == data[i].roleid){
								chkArray[j].checked = true;
								break;
							}
       			 		}//for
					}
				}//for
			});
		}
	});
}

/**
* @todo：用户保存（添加或编辑）
* @date：10:39 2008-2-22
* @author：hyh
*/
function saveUser(){
	DWRUtil.useLoadingMessage("正在保存用户信息...");
	var oJSUserBean = new JSUserBean();
	getFormValue(oJSUserBean);
	var deptId = DWRUtil.getValue("deptid");//deptid
	var dutyId = DWRUtil.getValue("dutyid");//dutyid
	var roleIds = getCheckValuesByName("rolechkbox");//roleid many
	if(oJSUserBean.account == ""){
		alert("用户帐号不能为空！");
		$("account").focus();
		return false;
	}
	if(oJSUserBean.realname == ""){
		alert("真实姓名不能为空！");
		$("realname").focus();
		return false;
	}
	if(deptId == ""){
		alert("请选择部门！");
		return false;
	}
	if(roleIds == ""){
		alert("请选择角色！");
		return false;
	}
	DWREngine.setAsync(false);
	if(!checkUserAccount(oJSUserBean)){
		alert("此用户账号已经存在！");
		$("account").focus();
		return false;
	}
	if(oJSUserBean.id == ""){//新增
		ConfigAction.insertAcluser(oJSUserBean,deptId,dutyId,roleIds,function(data){
			alert(data);
			if(JSRequest("deptid")){//dept add user
				parent.loadAcluserRelaAclorgList();
			}else{
				parent.loadAcluserList();
			}
			sClosed();
		});
	}else{//编辑
		ConfigAction.updateAcluser(oJSUserBean,deptId,dutyId,roleIds,function(data){
			if(JSRequest("deptid") && JSRequest("pagetype") == "org"){//dept add user
				parent.loadAcluserRelaAclorgList();
			}else if(JSRequest("pagetype") == "user"){
				parent.loadAcluserList();
			}else if(JSRequest("pagetype") == "role"){
				parent.loadAcluserByAclrole();
			}else if(JSRequest("pagetype") == "userquery"){
				parent.pageInit();
			}
			setUserDetailHtml();
		});
	}
	DWREngine.setAsync(true);
	//showBeanValue(oJSUserBean);
}

/**
* @todo：校验用户账号唯一性
* @date：2008-12-22 
* @author：wangchen
*/
function checkUserAccount(oJSUserBean){
	var result = true;
	ConfigAction.checkUserAccount(oJSUserBean,function(data){
		result = data;
	});
	return result;
}

//@see next js function is for user common function 17:20 2008-2-21

/**org query 16:24 2008-2-26*/
function openOrgQueryHtml(){
	var url = "aclorgquery.html";
	openWin_h(url,'300','380');
}

/**User search Object 17:20 2008-2-21*/
function JSUserSearchBean(){
	this.account = "";
	this.realname = "";
}

/**User Object 17:37 2008-2-22*/
/** modified by pxw 9:46 2008-03-04 */
function JSUserBean(){
	this.id = "";
	this.account = "";
	this.realname = "";
	this.password = "";
	this.userstatus = "";
	//this.lastmodifydate = "";
	//this.modifypassworddate = "";
	this.isonline = "";
	//this.createdate = "";
	this.remark = "";
	this.lastlogoninfoid = "";
	this.pas = "";
	this.mobile = "";
	this.telphone = "";
	this.userlevel="";
}

/**展示详情页面 9:26 2008-2-21*/
function setUserDetailHtml(){
	$("userop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">用户信息";
	$("op_button").innerHTML = $("div_hidden_detail").innerHTML;
	setInputReadOnly("account");
	setInputReadOnly("realname");
	setInputReadOnly("dutyid");
	setInputReadOnly("userlevel");
	setInputReadOnly("userstatus");
	setInputReadOnly("pas");
	setInputReadOnly("mobile");
	setInputReadOnly("telphone");
	setInputReadOnly("remark");
}

/**展示编辑页面 9:26 2008-2-21*/
function setUserEditHtml(){
	$("userop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">编辑用户";
	$("op_button").innerHTML = $("div_hidden_edit").innerHTML;
	setInputReadOnly("account","false");
	setInputReadOnly("realname","false");
	setInputReadOnly("dutyid","false");
	setInputReadOnly("userlevel","false");
	setInputReadOnly("userstatus","false");
	setInputReadOnly("pas","false");
	setInputReadOnly("mobile","false");
	setInputReadOnly("telphone","false");
	setInputReadOnly("remark","false");
	
	//角色列表checkbox可编辑
	$("rolechkall").disabled = false;
	var oArray = document.getElementsByName("rolechkbox");
	if(oArray.length){
		for(var i = 0 ; i < oArray.length ; i++ ){
			oArray[i].disabled = false;
		}
	}	
}